1 package org.thema;
2
3 import java.util.regex.Pattern;
4
5 /***
6 * Classe com métodos utilitários para serem utilizados em qualquer projeto
7 *
8 * @author Eduardo M. Sasso
9 * @since Jan 12, 2004
10 */
11 public class Util {
12
13 /***
14 *
15 * @param str - String a ser substituída
16 * @param oldStr - String antiga
17 * @param newStr - String nova
18 * @param all - Todas as ocorrências
19 * @return Nova string substituindo oldStr por newStr
20 */
21 public static String replace(String str, String oldStr, String newStr, boolean all) {
22 Pattern pattern = Pattern.compile(oldStr,Pattern.CASE_INSENSITIVE);
23
24 String resultado = str;
25
26 if (pattern.matcher(str).find()) {
27 if (all) {
28 resultado = pattern.matcher(str).replaceAll(newStr);
29 } else {
30 resultado = pattern.matcher(str).replaceFirst(newStr);
31 }
32 }
33 return resultado;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 }
58
59
60
61
62 public static String getTableName(String sql){
63 int begin = sql.toLowerCase().indexOf("from")+4;
64 int end = sql.toLowerCase().indexOf("where");
65 end = end==-1?sql.length():end;
66 String tableName = sql.substring(begin, end).trim();
67 return tableName;
68 }
69
70
71 }